package com.soft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soft.entity.Department;
import com.soft.entity.Student;
import com.soft.entity.Class;
import com.soft.mapper.ClassMapper;
import com.soft.mapper.DepartmentMapper;
import com.soft.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private StudentService studentService;
    @Autowired ClassService classService;
    //查询所有的系
    public List<Department> queryAll() {
        return departmentMapper.selectList(null);
    }
    //插入学生
    public int insert(String name,String address){
        Department department = new Department();
        department.setName(name);
        department.setAddress(address);
        return departmentMapper.insert(department);
    }
    //根据学生id，更新名字
    public int updateNameById(int id,String name){
        QueryWrapper<Department> departmentWrapper=new QueryWrapper<>();
        departmentWrapper.eq("id",id);
        Department department=departmentMapper.selectOne(departmentWrapper);
        department.setName(name);
        return departmentMapper.update(department,departmentWrapper);
    }
    //根据学生Id，删除学生
    public int deleteById(int id){
        QueryWrapper<Department> departmentWrapper=new QueryWrapper<>();
        departmentWrapper.eq("id",id);
        return departmentMapper.delete(departmentWrapper);
    }
    //根据学生姓名，搜索系名
    public String queryDepartmentNameByStudentName(String studentName){
        QueryWrapper<Student> studentWrapper=new QueryWrapper<>();
        studentWrapper.eq("name",studentName);
        Student student=studentMapper.selectOne(studentWrapper);
        QueryWrapper<Class> classWrapper=new QueryWrapper<>();
        classWrapper.eq("id",student.getClass_id());
        Class class_=classMapper.selectOne(classWrapper);
        QueryWrapper<Department> departmentWrapper=new QueryWrapper<>();
        departmentWrapper.eq("id",class_.getDepartmentId());
        Department department=departmentMapper.selectOne(departmentWrapper);
        return department.getName();

    }
    //统计全系人数
    public int countStudentNumbersByDepartmentName(String departmentName){
        QueryWrapper<Department> departmentWrapper=new QueryWrapper<>();
        departmentWrapper.eq("name",departmentName);
        Department department=departmentMapper.selectOne(departmentWrapper);
        QueryWrapper<Class> classWrapper=new QueryWrapper<>();
        classWrapper.eq("department_id",department.getId());
        List<Class> classList=classMapper.selectList(classWrapper);
        int count=0;
        for(Class clazz:classList){
            QueryWrapper<Student> studentWrapper=new QueryWrapper<>();
            studentWrapper.eq("class_id",clazz.getId());
            List<Student> studentList=studentMapper.selectList(studentWrapper);
            count+=studentList.size();
        }
        return count;
    }

}
